package com.ldu.controller;

import com.ldu.dao.query.support.PageResult;
import com.ldu.enums.CommonStatusEnum;
import com.ldu.pojo.*;
import com.ldu.service.*;
import com.ldu.util.*;

import com.ldu.service.NoticeService;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 2018-3-7 10:40:38
 *
 * @author pipi
 */
@Controller
@RequestMapping(value = "/admin")
@Slf4j
public class AdminController {

    @Resource
    private UserService userService;


    @Resource
    private GoodsService goodsService;

    @Resource
    private OrdersService ordersService;

    @Resource
    private PurseService purseService;

    @Resource
    private AdminService adminService;

    @Resource
    private NoticeService noticeService;

    @Resource
    private CatelogService catelogService;


    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(HttpSession session) {
        // 清除session
        session.invalidate();
        return "/admin/login";
    }

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(HttpServletRequest request, Admin admins) {
        Admin myadmin = adminService.findAdmin(admins.getPhone(), admins.getPassword());
        if (myadmin != null) {
            request.getSession().setAttribute("admin", myadmin);
            return "/admin/index";
        }
        return "/admin/login";

    }

    @RequestMapping(value = "/indexs", method = RequestMethod.GET)
    public String indexs(HttpServletRequest request) {
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        if (admin != null) {
            Long id = admin.getId();
            Admin myadmin = adminService.findAdminById(id);
            request.getSession().setAttribute("admin", myadmin);
            return "/admin/index";
        }
        return "/admin/login";
    }


    /**
     * <p> 查询个人信息 </p>
     *
     * @param request
     * @return ModelAndView
     * @throws
     * @author 皮皮 (pipi@maihaoche.com)
     * @date 2019/3/30 1:33 AM
     */
    @RequestMapping(value = "/info")
    @ResponseBody
    public ModelAndView getInfo(HttpServletRequest request) {
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("admin", admin);
        modelAndView.setViewName("admin/info");
        return modelAndView;
    }

    /**
     * <p> 获取当前登录者信息 </p>
     *
     * @param request
     * @return ModelAndView
     * @throws
     * @author 皮皮 (pipi@maihaoche.com)
     * @date 2019/3/30 1:34 AM
     */
    @RequestMapping(value = "/modify")
    @ResponseBody
    public ModelAndView getModify(HttpServletRequest request) {
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("admin", admin);
        modelAndView.setViewName("admin/modify");
        return modelAndView;
    }

    /**
     * <p> 修改密码 </p>
     *
     * @param request
     * @param admin
     * @return ModelAndView
     * @throws
     * @author 皮皮 (pipi@maihaoche.com)
     * @date 2019/3/30 1:34 AM
     */
    @RequestMapping(value = "/changePassword")
    @ResponseBody
    public ModelAndView changePassword(HttpServletRequest request, Admin admin) {
        String pwd = request.getParameter("password1");
        ModelAndView modelAndView = new ModelAndView();
        Admin admins = (Admin) request.getSession().getAttribute("admin");

        if (admin.getPassword().equals(admins.getPassword())) {
            admins.setPassword(pwd);
            adminService.updateAdmin(admins);
        } else {
            modelAndView.addObject("msg", "原密码有误，请重新输入！");
            modelAndView.setViewName("admin/modify");
            return modelAndView;
        }
        modelAndView.setViewName("admin/login");
        return modelAndView;
    }


    /**********************************************************
     *管理员管理 ：1查找管理员、2.增加管理员、3.刪除管理员 、 4.修改管理员
     *
     **********************************************************/
    @RequestMapping(value = "/adminList")
    @ResponseBody
    public ModelAndView getAdminList(@RequestParam("pageNum") int pageNum) {
        ModelAndView modelAndView = new ModelAndView();
        int pageSize = 5;
        int total = adminService.getAdminList().size();
        PageResult<Admin> pageAdmin = adminService.getPageAdmin(pageNum, pageSize);
        AdminGrid adminGrid = new AdminGrid();
        adminGrid.setCurrent(pageNum);
        adminGrid.setRowCount(pageSize);
        adminGrid.setRows(pageAdmin.getResult());
        adminGrid.setTotal(total);
        modelAndView.addObject("adminGrid", adminGrid);
        modelAndView.setViewName("admin/admin/admin_list");
        return modelAndView;
    }

    /**
     * <p> 获取管理员 </p>
     *
     * @param id
     * @return Admin
     * @throws
     * @author 皮皮 (pipi@maihaoche.com)
     * @date 2019/3/30 1:33 AM
     */
    @RequestMapping(value = "/getAdmin", method = RequestMethod.GET)
    @ResponseBody
    public Admin getAdmin(Integer id) {
        return adminService.getAdminById(id);
    }

    /**
     * <p> 删除管理员 </p>
     */
    @RequestMapping(value = "/deleteAdmin", method = RequestMethod.POST)
    @ResponseBody
    public Result deleteAdmin(HttpServletRequest request, @RequestParam(value = "id") Long id) {
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        if (CommonStatusEnum.NO.getCode().equals(admin.getUserRole())) {
            if (admin.getId().equals(id)) {
                return Result.create(true, "无法删除自己的账号！");
            }

            adminService.deleteAdminById(id);
            return Result.create(true, "删除成功！");
        } else {
            return Result.create(true, "你不是超级管理员！");
        }

    }

    /**
     * <p> 增加管理员 </p>
     *
     * @param request
     * @param admin
     * @return Result
     * @throws
     * @author 皮皮 (pipi@maihaoche.com)
     * @date 2019/3/30 1:32 AM
     */
    @RequestMapping(value = "/addAdmin", method = RequestMethod.POST)
    @ResponseBody
    public Result addAdmin(HttpServletRequest request, @RequestBody Admin admin) {
        Admin currentAdmin = (Admin) request.getSession().getAttribute("admin");
        if (!CommonStatusEnum.NO.getCode().equals(currentAdmin.getUserRole())) {
            return Result.create(false, "你不是超级管理员");
        }

        if (null == admin.getPhone()) {
            return Result.create(false, "账号不能为空");
        }
        if (StringUtils.isBlank(admin.getPassword()) || StringUtils.isBlank(admin.getRelPassword())) {
            return Result.create(false, "密码不能为空!");
        }
        if (!admin.getPassword().equals(admin.getRelPassword())) {
            return Result.create(false, "两次输入的密码不一致");
        }
        Admin queryAdmin = new Admin();
        queryAdmin.setPhone(admin.getPhone());
        List<Admin> admins = adminService.queryAdmin(queryAdmin);
        if (CollectionUtils.isNotEmpty(admins)) {
            return Result.create(false, "该账号已存在");
        }
        if (adminService.add(admin)) {
            return Result.create(true, "添加成功");
        } else {
            return Result.create(false, "添加失败");
        }
    }


    /* 修改管理员 */
    @RequestMapping(value = "/updateAdmin", method = RequestMethod.POST)
    @ResponseBody
    public Result updateAdmin(HttpServletRequest request, @RequestBody Admin admin) {
        Admin admins = adminService.selectByPrimaryKey(admin.getId());
        admins.setUsername(admin.getUsername());
        admins.setPassword(admin.getPassword());
        admins.setUserRole(admin.getUserRole());
        try {
            adminService.updateByPrimaryKey(admins);
            return Result.create(true, "修改成功！");
        } catch (Exception e) {
            return Result.create(false, "修改失败！");
        }
    }

    /**
     * <p> 查询管理员 </p>
     *
     * @param admin
     * @return ModelAndView
     * @throws
     * @author 皮皮 (pipi@maihaoche.com)
     * @date 2019/3/30 1:34 AM
     */
    @RequestMapping(value = "/searchAdmin", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView searchAdmin(Admin admin) {
        ModelAndView mv = new ModelAndView();
        int pageNum = 1;
        int pageSize = 5;
        int total = adminService.getAdminList().size();
        Long id = admin.getId();
        String username = admin.getUsername();
        Integer userRole = admin.getUserRole();

        PageResult<Admin> pageResult = adminService.getPageAdminByIdOrUserName(id, username, userRole, pageNum, pageSize);
        AdminGrid adminGrid = new AdminGrid();
        Admin searchAdmin = new Admin();
        searchAdmin.setId(id);
        searchAdmin.setUsername(username);
        searchAdmin.setUserRole(userRole);
        adminGrid.setCurrent(pageNum);
        adminGrid.setRowCount(pageSize);
        adminGrid.setRows(pageResult.getResult());
        adminGrid.setTotal(total);
        mv.addObject("adminGrid", adminGrid);
        mv.addObject("searchAdmin", searchAdmin);
        mv.setViewName("admin/admin/admin_list");
        return mv;
    }

    /**********************************************************
     *留言管理 ：1查找留言、2.查看留言、3.刪除留言
     *
     **********************************************************/

    @RequestMapping(value = "/noticeList")
    @ResponseBody
    public ModelAndView getAllNoticeList(@RequestParam("pageNum") int pageNum) {
        ModelAndView modelAndView = new ModelAndView();
        int pageSize = 5;
        int total = noticeService.getNoticeNum();
        List<Notice> rows = noticeService.getPageNotice(pageNum, pageSize);
        NoticeGrid noticeGrid = new NoticeGrid();
        noticeGrid.setCurrent(pageNum);
        noticeGrid.setRowCount(pageSize);
        noticeGrid.setRows(rows);
        noticeGrid.setTotal(total);
        modelAndView.addObject("noticeGrid", noticeGrid);
        modelAndView.setViewName("admin/notice/notice_list");
        return modelAndView;
    }

    /**
     * <p> 查看留言 </p>
     *
     * @return null
     * @throws
     * @author 皮皮 (pipi@maihaoche.com)
     * @date 2019/3/30 1:35 AM
     */
    @RequestMapping(value = "/getNotice")
    @ResponseBody
    public Notice getNotice(HttpServletRequest request) {
        String id = request.getParameter("id");
        return noticeService.getNoticeById(Integer.parseInt(id));
    }

    /**
     * <p> 删除留言 </p>
     *
     * @return null
     * @throws
     * @author 皮皮 (pipi@maihaoche.com)
     * @date 2019/3/30 1:35 AM
     */
    @RequestMapping(value = "/deleteNotice", method = RequestMethod.POST)
    @ResponseBody
    public String deleteNotice(@RequestParam(value = "id") String id) {
        noticeService.deleteNoticeByPrimaryKeys(Integer.parseInt(id));
        return "{\"success\":true,\"msg\":\"删除成功!\"}";
    }

    /**********************************************************
     *分类管理 ：1查找分类、2.查看分类、3.修改分类
     *
     **********************************************************/

    @RequestMapping(value = "/cateList")
    @ResponseBody
    public ModelAndView getCatelogList(@RequestParam("pageNum") int pageNum) {
        ModelAndView modelAndView = new ModelAndView();
        int pageSize = 5;
        int total = catelogService.getCatelogNum();
        PageResult<Catelog> pageCatelog = catelogService.getPageCatelog(pageNum, pageSize);
        CatelogGrid catelogGrid = new CatelogGrid();
        catelogGrid.setCurrent(pageNum);
        catelogGrid.setRowCount(pageSize);
        catelogGrid.setRows(pageCatelog.getResult());
        catelogGrid.setTotal(total);
        modelAndView.addObject("catelogGrid", catelogGrid);
        modelAndView.setViewName("admin/catelog/cate_list");
        return modelAndView;
    }

    /**
     * <p> 查看分类 </p>
     *
     * @param request
     * @return Catelog
     * @throws
     * @author 皮皮 (pipi@maihaoche.com)
     * @date 2019/3/30 1:36 AM
     */
    @RequestMapping(value = "/getCatelog")
    @ResponseBody
    public Catelog getCatelog(HttpServletRequest request) {
        String id = request.getParameter("id");
        return catelogService.getCatelogById(Long.valueOf(id));
    }

    /**
     * <p> 修改分类 </p>
     *
     * @param catelog
     * @return String
     * @throws
     * @author 皮皮 (pipi@maihaoche.com)
     * @date 2019/3/30 1:36 AM
     */
    @RequestMapping(value = "/updateCatelog", method = RequestMethod.POST)
    @ResponseBody
    public String updateCatelog(Catelog catelog) {
        Catelog catelogs = catelogService.selectByPrimaryKey(catelog.getId());
        catelogs.setName(catelog.getName());
        catelogs.setNumber(catelog.getNumber());
        catelogs.setStatus(catelog.getStatus());
        try {
            catelogService.updateByPrimaryKey(catelogs);
        } catch (Exception e) {
            return "{\"success\":false,\"msg\":\"保存失败!\"}";
        }
        return "{\"success\":true,\"msg\":\"保存成功!\"}";
    }

    /**
     * <p> 查询分类 </p>
     *
     * @param catelog
     * @return ModelAndView
     * @throws
     * @author 皮皮 (pipi@maihaoche.com)
     * @date 2019/3/30 1:35 AM
     */
    @RequestMapping(value = "/searchCatelog", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView searchCatelog(Catelog catelog) {
        ModelAndView mv = new ModelAndView();
        int pageNum = 1;
        int pageSize = 5;
        int total = catelogService.getCatelogNum();
        Long id = catelog.getId();
        String name = catelog.getName();

        List<Catelog> rows = catelogService.getPageCatelogByIdOrName(id, name, pageNum, pageSize);
        CatelogGrid catelogGrid = new CatelogGrid();
        Catelog searchCatelog = new Catelog();
        searchCatelog.setId(id);
        searchCatelog.setName(name);

        catelogGrid.setCurrent(pageNum);
        catelogGrid.setRowCount(pageSize);
        catelogGrid.setRows(rows);
        catelogGrid.setTotal(total);
        mv.addObject("catelogGrid", catelogGrid);
        mv.addObject("searchCatelog", searchCatelog);
        mv.setViewName("admin/catelog/cate_list");
        return mv;
    }


    @RequestMapping(value = "addCotelog", method = RequestMethod.POST)
    @ResponseBody
    public Result addCotelog(@RequestBody Catelog catelog) {
        try {
            catelogService.addCatelog(catelog);
            return Result.create(true, "添加成功");
        } catch (Exception e) {
            log.error("添加是吧", e);
            return Result.create(false, e.getMessage());
        }
    }

//    @RequestMapping(value = "doDelete", method = RequestMethod.GET)
//    @ResponseBody
//    public Result doDelete(Long id) {
//        try {
//            catelogService.deleteCatelog(id);
//            return Result.create(true, "删除成功");
//        } catch (Exception e) {
//            log.error("删除失败", e);
//            return Result.create(false, "删除失败");
//
//        }
//
//    }


    @RequestMapping(value = "updateStatus", method = RequestMethod.GET)
    @ResponseBody
    public Result updateStatus(Long id, Integer status) {
        try {
            Catelog catelog = new Catelog();
            catelog.setId(id);
            catelog.setStatus(status);
            catelogService.updateByPrimaryKey(catelog);
            return Result.create(true, "成功");
        } catch (Exception e) {
            log.error("失败", e);
            return Result.create(false, "失败");

        }

    }

    /*********************************************************
     * 用户管理 1.查找所有用户 2.查看用户 3.修改用户 4.删除用户 5.查询用户
     *
     **********************************************************/

    /* 查找所有用户 */
    @RequestMapping(value = "/userList")
    @ResponseBody
    public ModelAndView getUserList(@RequestParam("pageNum") int pageNum) {
        ModelAndView modelAndView = new ModelAndView();
        int pageSize = 5;
        int total = userService.getUserNum();
        List<User> rows = userService.getPageUser(pageNum, pageSize);
        UserGrid userGrid = new UserGrid();
        userGrid.setCurrent(pageNum);
        userGrid.setRowCount(pageSize);
        userGrid.setRows(rows);
        userGrid.setTotal(total);
        modelAndView.addObject("userGrid", userGrid);
        modelAndView.setViewName("admin/user/user_list");
        return modelAndView;
    }

    /* 查看用户 */
    @RequestMapping(value = "/getUser")
    @ResponseBody
    public User getUser(HttpServletRequest request) {
        String id = request.getParameter("id");
        User user = userService.getUserById(Integer.parseInt(id));
        return user;
    }

    /* 修改用户 */
    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    @ResponseBody
    public String updateUser(HttpServletRequest request, User user) {
        User users = userService.selectByPrimaryKey(user.getId());
        user.setPassword(users.getPassword());
        try {
            userService.updateUserName(user);
        } catch (Exception e) {
            return "{\"success\":false,\"msg\":\"保存失败!\"}";
        }
        return "{\"success\":true,\"msg\":\"保存成功!\"}";
    }

    /**
     * <p> 删除用户 </p>
     *
     * @param request
     * @param ids
     * @return String
     * @throws
     * @author 皮皮 (pipi@maihaoche.com)
     * @date 2019/3/30 1:36 AM
     */
    @RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
    @ResponseBody
    public String deleteUser(HttpServletRequest request, @RequestParam(value = "ids[]") String[] ids) {
        try {
            for (int i = 0; i < ids.length; i++) {
                userService.deleteUserById(ids[i]);
            }
        } catch (Exception e) {
            return "{\"success\":false,\"msg\":\"删除失败!\"}";
        }
        return "{\"success\":true,\"msg\":\"删除成功!\"}";
    }

    /* 查询用户 */
    @RequestMapping(value = "/searchUser", method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView searchUser(HttpServletRequest request, User user) {
        ModelAndView mv = new ModelAndView();
        int pageNum = 1;
        int pageSize = 5;
        int total = userService.getUserNum();
        String phone = user.getPhone();
        String username = user.getUsername();
        String qq = user.getQq();
        List<User> rows = userService.getPageUserByUser(phone, username, qq, pageNum, pageSize);
        UserGrid userGrid = new UserGrid();
        User searchuser = new User();
        searchuser.setPhone(phone);
        searchuser.setUsername(username);
        searchuser.setQq(qq);
        userGrid.setCurrent(pageNum);
        userGrid.setRowCount(pageSize);
        userGrid.setRows(rows);
        userGrid.setTotal(total);
        mv.addObject("userGrid", userGrid);
        mv.addObject("searchuser", searchuser);
        mv.setViewName("admin/user/user_list");
        return mv;
    }

    /*********************************************************
     * 商品管理 1.查找所有商品 2.查看商品 3.修改商品 4.删除商品 5.查询商品 lyq
     *
     **********************************************************/

    /* 查找所有商品 */
    @RequestMapping(value = "/goodsList")
    @ResponseBody
    public ModelAndView getGoodsList(@RequestParam("pageNum") int pageNum) {
        ModelAndView mv = new ModelAndView();
        int pageSize = 5;
        int total = goodsService.getGoodsNum();
        List<Goods> rows = goodsService.getPageGoods(pageNum, pageSize);
        GoodsGrid goodsGrid = new GoodsGrid();
        goodsGrid.setCurrent(pageNum);
        goodsGrid.setRowCount(pageSize);
        goodsGrid.setRows(rows);
        goodsGrid.setTotal(total);
        List<Catelog> list = catelogService.getCatelogList();
        mv.addObject("goodsGrid", goodsGrid);
        mv.addObject("catelogList", list);
        mv.setViewName("admin/goods/goods_list");
        return mv;
    }

    /* 查看商品 */
    @RequestMapping(value = "/getGoods")
    @ResponseBody
    public Goods getGoods(HttpServletRequest request) {
        String id = request.getParameter("id");
        Goods goods = goodsService.getGoodsById(Integer.parseInt(id));
        return goods;
    }

    /* 修改商品 */
    @RequestMapping(value = "/updateGoods", method = RequestMethod.POST)
    @ResponseBody
    public Result updateGoods(HttpServletRequest request, Goods goods) {
        int id = goods.getId();
        Goods oldGoods = goodsService.getGoodsById(id);
        goods.setUserId(oldGoods.getUserId());
        goods.setPolishTime(oldGoods.getPolishTime());
        goods.setEndTime(oldGoods.getEndTime());
        try {
            goodsService.updateGoodsByPrimaryKeyWithBLOBs(goods.getId(), goods);
        } catch (Exception e) {
            return Result.create(false, "修改失败！");
        }
        return Result.create(true, "保存成功");
    }

    /* 删除商品 */
    @RequestMapping(value = "/deleteGoods", method = RequestMethod.POST)
    @ResponseBody
    public String deleteGoods(HttpServletRequest request, @RequestParam(value = "ids[]") String[] ids) {
        try {
            for (int i = 0; i < ids.length; i++) {
                goodsService.deleteGoodsByPrimaryKeys(Integer.parseInt(ids[i]));
                //focus
            }
        } catch (Exception e) {
            return "{\"success\":false,\"msg\":\"删除失败!\"}";
        }
        return "{\"success\":true,\"msg\":\"删除成功!\"}";
    }

    /* 查询商品 */
    @RequestMapping(value = "/searchGoods", method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView searchGoods(HttpServletRequest request, Goods goods) {
        ModelAndView mv = new ModelAndView();
        int pageNum = 1;
        int pageSize = 5;
        int total = goodsService.getGoodsNum();
        Integer id = goods.getId();
        String name = goods.getName();
        Integer status = goods.getStatus();
        if (status == -1) {
            status = null;
        }
        List<Goods> rows = goodsService.getPageGoodsByGoods(id, name, status, pageNum, pageSize);
        GoodsGrid goodsGrid = new GoodsGrid();
        Goods searchgoods = new Goods();
        searchgoods.setId(id);
        searchgoods.setName(name);
        searchgoods.setStatus(status);
        goodsGrid.setCurrent(pageNum);
        goodsGrid.setRowCount(pageSize);
        goodsGrid.setRows(rows);
        goodsGrid.setTotal(total);
        mv.addObject("goodsGrid", goodsGrid);
        mv.addObject("searchgoods", searchgoods);
        mv.setViewName("admin/goods/goods_list");
        return mv;
    }

    /*********************************************************
     * 订单管理 1.查找所有订单 2.查看订单 3.修改订单 4.删除订单 5.查询订单 lyq
     *
     **********************************************************/

    /* 查找所有订单 */
    @RequestMapping(value = "/ordersList")
    @ResponseBody
    public ModelAndView getOrdersList(@RequestParam("pageNum") int pageNum) {
        ModelAndView modelAndView = new ModelAndView();
        int pageSize = 5;
        int total = ordersService.getOrdersNum();
        List<Orders> rows = ordersService.getPageOrders(pageNum, pageSize);
        OrdersGrid ordersGrid = new OrdersGrid();
        ordersGrid.setCurrent(pageNum);
        ordersGrid.setRowCount(pageSize);
        ordersGrid.setRows(rows);
        ordersGrid.setTotal(total);
        modelAndView.addObject("ordersGrid", ordersGrid);
        modelAndView.setViewName("admin/orders/orders_list");
        return modelAndView;
    }

    /* 查看订单 */
    @RequestMapping(value = "/getOrders")
    @ResponseBody
    public Orders getOrders(HttpServletRequest request) {
        String id = request.getParameter("id");
        Orders orders = ordersService.getOrdersById(Integer.parseInt(id));
        return orders;
    }

    /* 修改订单 */
    @RequestMapping(value = "/updateOrders", method = RequestMethod.POST)
    @ResponseBody
    public String updateOrders(HttpServletRequest request, Orders orders) {
        int id = orders.getId();
        Orders oldorders = ordersService.getOrdersById(id);
        orders.setGoodsId(oldorders.getGoodsId());
        orders.setUserId(oldorders.getUserId());
        Goods goods = goodsService.getGoodsById(oldorders.getGoods().getId());
        // todo 什么吊逻辑？？？
        if (oldorders.getOrderState() != orders.getOrderState()) {
            Float balance = orders.getOrderPrice();
            if (orders.getOrderState() == 3) {
                purseService.updatePurseByuserId(goods.getUserId(), balance);
            } else {
                purseService.updatePurseOfdel(goods.getUserId(), balance);
            }
        }
        try {
            ordersService.updateByPrimaryKey(id, orders);
        } catch (Exception e) {
            return "{\"success\":false,\"msg\":\"保存失败!\"}";
        }
        return "{\"success\":true,\"msg\":\"保存成功!\"}";
    }

    /* 删除订单 */
    @RequestMapping(value = "/deleteOrders", method = RequestMethod.POST)
    @ResponseBody
    public String deleteOrders(HttpServletRequest request, @RequestParam(value = "ids[]") String[] ids) {
        try {
            for (int i = 0; i < ids.length; i++) {
                ordersService.deleteOrdersByPrimaryKeys(Integer.parseInt(ids[i]));
            }
        } catch (Exception e) {
            return "{\"success\":false,\"msg\":\"删除失败!\"}";
        }
        return "{\"success\":true,\"msg\":\"删除成功!\"}";
    }

    /* 查询订单 */
    @RequestMapping(value = "/searchOrders", method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView searchOrders(HttpServletRequest request, Orders orders) {
        ModelAndView mv = new ModelAndView();
        int pageNum = 1;
        int pageSize = 5;
        int total = ordersService.getOrdersNum();
        Long orderNum = orders.getOrderNum();
        String orderInformation = orders.getOrderInformation();
        Integer orderState = orders.getOrderState();
        List<Orders> rows = ordersService.getPageOrdersByOrders(orderNum, orderInformation, orderState, pageNum, pageSize);
        OrdersGrid ordersGrid = new OrdersGrid();
        Orders searchorders = new Orders();
        searchorders.setOrderNum(orderNum);
        searchorders.setOrderInformation(orderInformation);
        searchorders.setOrderState(orderState);
        ordersGrid.setCurrent(pageNum);
        ordersGrid.setRowCount(pageSize);
        ordersGrid.setRows(rows);
        ordersGrid.setTotal(total);
        mv.addObject("ordersGrid", ordersGrid);
        mv.addObject("searchorders", searchorders);
        mv.setViewName("admin/orders/orders_list");
        return mv;
    }


    /*********************************************************
     * 钱包管理 1.查找所有钱包 2.查看钱包 3.修改钱包 4.删除钱包 5.查询钱包 lyq
     *
     **********************************************************/

    /* 查找所有钱包 */
    @RequestMapping(value = "/purseList")
    @ResponseBody
    public ModelAndView getPurseList(HttpServletRequest request) {
        int pageNum = Integer.parseInt(request.getParameter("pageNum"));
        ModelAndView modelAndView = new ModelAndView();
        int pageSize = 5;
        int total = purseService.getPurseNum();
        List<Purse> rows = purseService.getPagePurse(pageNum, pageSize);
        PurseGrid purseGrid = new PurseGrid();
        purseGrid.setCurrent(pageNum);
        purseGrid.setRowCount(pageSize);
        purseGrid.setRows(rows);
        purseGrid.setTotal(total);
        modelAndView.addObject("purseGrid", purseGrid);
        modelAndView.setViewName("admin/purse/purse_list");
        return modelAndView;
    }

    /* 查询钱包 */
    @RequestMapping(value = "/searchPurse", method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView searchPurse(HttpServletRequest request, Purse purse) {
        ModelAndView mv = new ModelAndView();
        int pageNum = 1;
        int pageSize = 5;
        int total = purseService.getPurseNum();
        Integer userId = purse.getUserId();
        Integer state = purse.getState();
        List<Purse> rows = purseService.getPagePurseByPurse(userId, state, pageNum, pageSize);
        PurseGrid purseGrid = new PurseGrid();
        Purse searchpurse = new Purse();
        searchpurse.setUserId(userId);
        searchpurse.setState(state);
        purseGrid.setCurrent(pageNum);
        purseGrid.setRowCount(pageSize);
        purseGrid.setRows(rows);
        purseGrid.setTotal(total);
        mv.addObject("purseGrid", purseGrid);
        mv.addObject("searchpurse", searchpurse);
        mv.setViewName("admin/purse/purse_list");
        return mv;
    }

    /* 查看订单 */
    @RequestMapping(value = "/getPurse")
    @ResponseBody
    public Purse getPurse(HttpServletRequest request) {
        String id = request.getParameter("id");
        Purse purse = purseService.getPurseById(Integer.parseInt(id));
        return purse;
    }



    /* 修改钱包 */
//	@RequestMapping(value = "/updatePurse", method = RequestMethod.POST)
//	@ResponseBody
//	public String updatePurse(HttpServletRequest request, Purse purse) {
//		int id = purse.getId();
//		try {
//			purseService.updateByPrimaryKey(id, purse);
//		} catch (Exception e) {
//			return "{\"success\":false,\"msg\":\"保存失败!\"}";
//		}
//		return "{\"success\":true,\"msg\":\"保存成功!\"}";
//	}

    /* 通过 */
    @RequestMapping(value = "/updatePursePass", method = RequestMethod.POST)
    @ResponseBody
    public String updatePursePass(HttpServletRequest request, Purse purse) {
        Float balance = purse.getBalance();
        purse.setState(2);
        try {
            if (purse.getRecharge() != null) {//充值 充值金额=null 当前金额=当前金额+充值金额
                Float recharge = purse.getRecharge();
                Float balanceRecharge = balance + recharge;
                purse.setBalance(balanceRecharge);
                purseService.updatePursePassById(purse.getId(), purse);
            }
            if (purse.getWithdrawals() != null) {//提现
                Float withdrawals = purse.getWithdrawals();
                Float balanceWithdrawals = balance - withdrawals;
                purse.setBalance(balanceWithdrawals);
                purseService.updatePurseRefuseById(purse.getId(), purse);
            }
        } catch (Exception e) {
            return "{\"success\":true,\"msg\":\"审核失败，请核对金额!\"}";
        }
        return "{\"success\":true,\"msg\":\"审核成功!\"}";
    }

    /* 不通过 */
    @RequestMapping(value = "/updatePurseRefuse", method = RequestMethod.POST)
    @ResponseBody
    public String updatePurseRefuse(HttpServletRequest request, Purse purse) {
        purse.setState(1);
        try {

            purseService.updatePurseRefuseById(purse.getId(), purse);

        } catch (Exception e) {
            return "{\"success\":true,\"msg\":\"审核失败!\"}";
        }
        return "{\"success\":true,\"msg\":\"审核成功!\"}";
    }

    /* 用户查看审核结果 */
    @RequestMapping(value = "/updatePurseState", method = RequestMethod.GET)
    public void updatePurseState(HttpServletRequest request) {
        Integer id = Integer.parseInt(request.getParameter("id"));
        Purse purse = purseService.getPurseById(id);
        purse.setState(null);
        this.purseService.updateByPrimaryKey(id, purse);//修改state为null
    }

}